package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    //默认添加的商品数量
    private static final int CUSTOMER_NUMBER = 1;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //新建一个entity用来存储到数据库中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        log.info("添加购物车:{}", shoppingCart);

        //判断当前商品是否在购物车中
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            //如果在就只需要把数目+1
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + CUSTOMER_NUMBER);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //如果不在就判断是套餐还是菜品然后添加到数据库中
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();

            if (dishId != null) {
                //如果是菜品
                //填充字段 通过菜品id查询数据
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setNumber(CUSTOMER_NUMBER);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
            } else if (setmealId != null) {
                //如果是套餐
                //填充字段
                SetmealVO sm = setmealMapper.getByIdWithDishBase(setmealId);
                shoppingCart.setName(sm.getName());
                shoppingCart.setImage(sm.getImage());
                shoppingCart.setNumber(CUSTOMER_NUMBER);
                shoppingCart.setAmount(sm.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            //最后插入数据
            log.info("最终的购物车信息为:{}", shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     */
    @Override
    public  List<ShoppingCart> list() {
        log.info("查询购物车");
        List<ShoppingCart> list = shoppingCartMapper.list(new ShoppingCart());
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean(ShoppingCart shoppingCart) {
        shoppingCartMapper.deleteByUserId(shoppingCart);
    }

    /**
     * 删除购物车的一个数据 通过用户id
     * @param shoppingCartDTO
     */
    @Override
    public void deleteBranthByid(ShoppingCartDTO shoppingCartDTO) {
        //需要判断当前菜品在购物车的份数
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());  //设置用户id 通过threadLocal

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断是否是空值
        if (list != null || list.size() > 0) {
            //如果是1 直接删除即可
            if (list.get(0).getNumber() == 1) {
                shoppingCartMapper.deleteByUserId(shoppingCart);
            } else {
                //如果在就只需要把数目+1
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber() - CUSTOMER_NUMBER);
                shoppingCartMapper.updateNumberById(cart);
            }

        }else {
            throw new RuntimeException("通过用户id删除数据的时候 传过来的参数没有包含菜品id或者是套餐id");
        }

    }
}
